home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / mach / ds5000.md / fcntl.c < prev    next >
C/C++ Source or Header  |  1989-07-24  |  4KB  |  188 lines

  1. /* 
  2.  * fcntl.c --
  3.  *
  4.  *    Procedure to map the Unix fcntl system call to Sprite.
  5.  *
  6.  * Copyright 1986 Regents of the University of California
  7.  * All rights reserved.
  8.  */
  9.  
  10. #ifndef lint
  11. static char rcsid[] = "$Header: /sprite/src/kernel/mach/ds3100.md/RCS/fcntl.c,v 1.2 89/07/24 19:52:45 nelson Exp $ SPRITE (Berkeley)";
  12. #endif not lint
  13.  
  14. #include "sprite.h"
  15. #include "fs.h"
  16.  
  17. #include "compatInt.h"
  18.  
  19. #include "user/fcntl.h"
  20. #include "machInt.h"
  21. #include "machConst.h"
  22. #include "mach.h"
  23.  
  24. extern Mach_State    *machCurStatePtr;
  25.  
  26.  
  27. /*
  28.  *----------------------------------------------------------------------
  29.  *
  30.  * fcntl --
  31.  *
  32.  *    Procedure to map from Unix fcntl system call to Sprite Fs_IOControl.
  33.  *
  34.  * Results:
  35.  *      Error returned if error, SUCCESS otherwise.
  36.  *
  37.  * Side effects:
  38.  *    Variable.
  39.  *
  40.  *----------------------------------------------------------------------
  41.  */
  42.  
  43. int
  44. MachUNIXFcntl(fd, cmd, arg)
  45.     int fd;        /* File to operate on. */
  46.     int cmd;        /* Type of command. */
  47.     int arg;        /* Optional argument to the command. */
  48. {
  49.     ReturnStatus status;
  50.     int value;
  51.     Address        usp;
  52.  
  53.     usp = (Address)machCurStatePtr->userState.regState.regs[SP];
  54.  
  55.     switch (cmd) {
  56.     case F_DUPFD:
  57.         usp -= sizeof(int);
  58.         status = Vm_CopyOut(sizeof(int), (Address)&arg, usp);
  59.         if (status != SUCCESS) {
  60.         return(status);
  61.         }
  62.         status = Fs_GetNewIDStub(fd, (int *)usp);
  63.         if (status == SUCCESS) {
  64.         (void)Vm_CopyIn(sizeof(int), usp,
  65.                 (Address)&machCurStatePtr->userState.unixRetVal);
  66.         }
  67.         break;
  68.  
  69.     case F_GETFD:
  70.         usp -= sizeof(int);
  71.         status = Fs_IOControlStub(fd, IOC_GET_FLAGS, 
  72.                 0, (Address) NULL, sizeof(value), usp);
  73.         if (status == SUCCESS) {
  74.         (void)Vm_CopyIn(sizeof(int), usp, 
  75.             (Address)&machCurStatePtr->userState.unixRetVal);
  76.         machCurStatePtr->userState.unixRetVal = 
  77.         (machCurStatePtr->userState.unixRetVal & IOC_CLOSE_ON_EXEC) ? 1 : 0;
  78.         }
  79.         break;
  80.  
  81.     case F_SETFD:
  82.         usp -= sizeof(int);
  83.         value = IOC_CLOSE_ON_EXEC;
  84.         status = Vm_CopyOut(sizeof(value), (Address)&value, usp);
  85.         if (status != SUCCESS) {
  86.         break;
  87.         }
  88.         if (arg & 1) {
  89.         status = Fs_IOControlStub(fd, IOC_SET_BITS, 
  90.                 sizeof(value), usp, 0, (Address) NULL);
  91.         } else {
  92.         status = Fs_IOControlStub(fd, IOC_CLEAR_BITS, 
  93.                 sizeof(value), usp, 0, (Address) NULL);
  94.         }
  95.         break;
  96.  
  97.     case F_GETFL:  {
  98.         int temp;
  99.  
  100.         usp -= sizeof(int);
  101.         status = Fs_IOControlStub(fd, IOC_GET_FLAGS, 
  102.                 0, (Address) NULL, sizeof(temp), usp);
  103.         if (status != SUCCESS) {
  104.             break;
  105.         }
  106.         (void)Vm_CopyIn(sizeof(temp), usp, (Address)&temp);
  107.         machCurStatePtr->userState.unixRetVal = 0;
  108.         if (temp & IOC_APPEND) {
  109.             machCurStatePtr->userState.unixRetVal |= FAPPEND;
  110.         }
  111.         if (temp & IOC_NON_BLOCKING) {
  112.             machCurStatePtr->userState.unixRetVal |= FNDELAY;
  113.         }
  114.         if (temp & IOC_ASYNCHRONOUS) {
  115.             machCurStatePtr->userState.unixRetVal |= FASYNC;
  116.         }
  117.         }
  118.         break;
  119.  
  120.     case F_SETFL:
  121.         value = 0;
  122.         if (arg & FAPPEND) {
  123.         value |= IOC_APPEND;
  124.         }
  125.         if (arg & FNDELAY) {
  126.         value |= IOC_NON_BLOCKING;
  127.         }
  128.         if (arg & FASYNC) {
  129.         value |= IOC_ASYNCHRONOUS;
  130.         }
  131.         if (value == 0) {
  132.         status = SUCCESS;
  133.         } else {
  134.         usp -= sizeof(int);
  135.         status = Vm_CopyOut(sizeof(value), &value, usp);
  136.         if (status != SUCCESS) {
  137.             break;
  138.         }
  139.         status = Fs_IOControlStub(fd, IOC_SET_BITS, 
  140.                 sizeof(value), usp, 0, (Address) NULL);
  141.         }
  142.         break;
  143.  
  144.     case F_GETOWN: {
  145.         Ioc_Owner owner;
  146.  
  147.         usp -= sizeof(owner);
  148.         status = Fs_IOControlStub(fd, IOC_GET_OWNER, 
  149.                       0, (Address) NULL,
  150.                       sizeof(owner), usp);
  151.         if (status != SUCCESS) {
  152.             break;
  153.         }
  154.         (void)Vm_CopyIn(sizeof(owner), usp, &owner);
  155.         if (owner.procOrFamily == IOC_OWNER_FAMILY) {
  156.             machCurStatePtr->userState.unixRetVal = -owner.id;
  157.         } else {
  158.             machCurStatePtr->userState.unixRetVal = owner.id;
  159.         }
  160.         }
  161.         break;
  162.  
  163.     case F_SETOWN: {
  164.         Ioc_Owner owner;
  165.  
  166.         usp -= sizeof(owner);
  167.         if (arg < 0) {
  168.             owner.id = -arg;
  169.             owner.procOrFamily = IOC_OWNER_FAMILY;
  170.         } else {
  171.             owner.id = arg;
  172.             owner.procOrFamily = IOC_OWNER_PROC;
  173.         }
  174.         status = Vm_CopyOut(sizeof(owner), (Address)&owner, usp);
  175.         if (status != SUCCESS) {
  176.             break;
  177.         }
  178.         status = Fs_IOControlStub(fd, IOC_SET_OWNER, 
  179.                 sizeof(owner), usp, 0, (Address) NULL);
  180.         }
  181.         break;
  182.  
  183.     default:
  184.         break;
  185.     }
  186.     return(status);
  187. }
  188.